package com.own.business.push.persistence.mysql.service.impl;

import com.own.business.push.persistence.mysql.entity.bo.PushRegistrationBo;
import com.own.business.push.persistence.mysql.entity.map.PushRegistrationMapVo;
import com.own.business.push.persistence.mysql.entity.po.PushRegistration;
import com.own.business.push.persistence.mysql.entity.query.PushRegistrationQuery;
import com.own.business.push.persistence.mysql.entity.vo.PushRegistrationVo;
import com.own.business.push.persistence.mysql.mapper.PushRegistrationMapper;
import com.own.business.push.persistence.mysql.service.PushRegistrationService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * PushRegistrationServiceImpl
 *
 * @author chenxueli
 * @date 2023-11-16 14:14:41
 */
@Slf4j
@Service
public class PushRegistrationServiceImpl extends AbstractBaseService<
        PushRegistration,
        PushRegistrationBo,
        PushRegistrationVo,
        PushRegistrationMapVo,
        PushRegistrationQuery,
        PushRegistrationMapper
        > implements PushRegistrationService {

    @Resource
    private PushRegistrationMapper pushRegistrationMapper;

    /**
     * 根据用户id删除
     *
     * @param userId 用户id
     */
    @Override
    public void deleteByUserId(Long userId) {
        var wrapper = lambdaQueryWrapper().eq(PushRegistration::getFkUserId, userId);
        pushRegistrationMapper.delete(wrapper);
    }

    /**
     * 根据regId和module删除
     *
     * @param regId  注册id
     * @param module 模块
     */
    @Override
    public void deleteByRegId(String regId, String module) {
        var wrapper = lambdaQueryWrapper().eq(PushRegistration::getRegId, regId).eq(PushRegistration::getModule, module);
        pushRegistrationMapper.delete(wrapper);
    }

    /**
     * 保存信息
     *
     * @param userId 用户id
     * @param regId  注册id
     * @param module 模块
     */
    @Override
    public void save(Long userId, String regId, String module) {
        var po = new PushRegistration();
        po.setFkUserId(userId);
        po.setRegId(regId);
        po.setModule(module);
        po.setGmtCreate(LocalDateTime.now());
        pushRegistrationMapper.insert(po);
    }

    /**
     * 根据用户id获取
     *
     * @param userId 用户id
     * @return 推送注册
     */
    @Override
    public PushRegistration getByUserId(Long userId) {
        var wrapper = lambdaQueryWrapper().eq(PushRegistration::getFkUserId, userId);
        return getOneByWrapper(wrapper);
    }

    /**
     * 根据用户ids获取
     *
     * @param userIdList 用户ids
     * @return 推送注册
     */
    @Override
    public List<PushRegistration> listByUserIdList(Collection<Long> userIdList) {
        if (userIdList == null || userIdList.isEmpty()) {
            return new ArrayList<>();
        }
        var wrapper = lambdaQueryWrapper().in(PushRegistration::getFkUserId, userIdList);
        return listByWrapper(wrapper);
    }

    /**
     * 根据模块查询注册id列表
     *
     * @param module 模块
     * @return 注册id列表
     */
    @Override
    public List<String> listRegIdListByModule(String module) {
        var wrapper = lambdaQueryWrapper().select(PushRegistration::getRegId).eq(PushRegistration::getModule, module);
        return listByWrapper(wrapper).stream().map(PushRegistration::getRegId).distinct().collect(Collectors.toList());
    }

}
